﻿using HslCommunication;
using HslCommunication.Profinet.Siemens;
using MyFile.Config;
using MyFile.Init;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static MyFile.Global_Event;

namespace MyFile.Service
{
    public class PLCFun: Global_Event
    {
        private static readonly Lazy<PLCFun> lazy = new Lazy<PLCFun>(() => new PLCFun());
        public static PLCFun Instance { get { return lazy.Value; } }
        public override event GlobalHandler GlobalEvent;
        Thread thread = null;
        public PLCFun() { }
        
        /// <summary>
        /// 触发事件的条件
        /// </summary>
        public void TriggerEvent()=>TriggerEvents(new EventArgs());
        /// <summary>
        /// 触发事件
        /// </summary>
        public void TriggerEvents(EventArgs e)
        {
            GlobalHandler handler = GlobalEvent;
            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }
        /// <summary>
        /// 事件处理函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void EventFun(object sender, EventArgs e)
        {
            foreach (DBData data in SiemensInit.Instance.config.S7DB)
            {
                //Console.Clear();
                Console.WriteLine($"{data.DBAddress}\t\t{data.Status}\r\n");
            }
        }
        public void Init()
        {
            thread = new Thread(ReadPLCData);
            GlobalEvent += EventFun;
        }
        public void Start()
        {
            thread.Start();
        }
        public void Close()
        {
           
        }
        #region 读取PLC数据
        /// <summary>
        /// 读取PLC数据
        /// </summary>
        public void ReadPLCData()
        {
            while (true)
            {
                SiemensS7Net s7Net = SiemensInit.Instance.service;
                if (s7Net == null)
                {
                    Thread.Sleep(1000);
                    continue;
                }
                try
                {
                    foreach (DBData data in SiemensInit.Instance.config.S7DB)
                    {
                        OperateResult<bool> result = s7Net.ReadBool(data.DBAddress);
                        if(result.IsSuccess)
                        {
                            if(result.Content != data.Status)
                            {
                                data.Status = result.Content;
                                Console.Clear();
                                //触发事件条件
                                TriggerEvent();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Thread.Sleep(1000);
            }
        }
        #endregion
    }
}
